﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Chat
{
    public abstract class CmdSerializer<InCmd,OutCmd> : InCmdOutCmd<InCmd,OutCmd>
        where InCmd : Command where OutCmd : Command
    {
        public InCmd DeserializeCommand(Stream serializationStream)
        {
            object ocmd = Deserialize(serializationStream);

            Command cmd = ocmd as Command;

            if (cmd == null)
                throw new UnknownCmdException(ocmd);

            InCmd inCmd = cmd as InCmd;

            if (inCmd == null)
                throw new UnexpectedCmdException(cmd);

            return inCmd;
        }

        public abstract void Serialize(Stream serializationStream, OutCmd command);

        protected abstract object Deserialize(Stream serializationStream);
    }
}
